#include<iostream>
#include<string>
#include<cstdint>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<cstring>
using namespace std;
int main(int argc,char*argv[])
{
    if(argc!=3)
    {
        cerr<<"Usage: ./udpclient ip port"<<endl;
        return 1;
    }
    string ip=argv[1];
    uint16_t port=stoi(argv[2]);
    int sockfd=socket(AF_INET,SOCK_DGRAM,0);
    if(sockfd<0)
    {
        cerr<<"socket error"<<endl;
        return 2;
    }
    struct sockaddr_in server;
    memset(&server,0,sizeof(server));
    server.sin_family=AF_INET;
    server.sin_port=htons(port);
    server.sin_addr.s_addr=inet_addr(ip.c_str());
    while(true)
    {
        string input;
        cout<<"Please Enter# ";
        getline(cin,input);
        ssize_t s=sendto(sockfd,input.c_str(),input.size()
        ,0,(struct sockaddr*)&server,sizeof(server));

        char buffer[1024];
        struct sockaddr_in peer;
        socklen_t len=sizeof(peer);
        ssize_t n=recvfrom(sockfd,buffer,sizeof(buffer),0
        ,(struct sockaddr*)&peer,&len);
        if(n>0)
        {
            buffer[n]=0;
            cout<<"server echo# "<<buffer<<endl;
        }
    }
    return 0;
}